package krow.dev.weathertest;

import java.util.*;

import com.example.weathertest.*;

import android.content.*;
import android.view.*;
import android.widget.*;

public class MyListAdapter extends BaseAdapter {
	CodeChange cd = new CodeChange();
	Context maincon;
	LayoutInflater Inflater;
	ArrayList<WeatherData> arSrc;
	int layout;

	public MyListAdapter(Context context, int alayout, ArrayList<WeatherData> aarSrc) {
		maincon = context;
		Inflater = (LayoutInflater)context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		arSrc = aarSrc;
		layout = alayout;
	}

	public int getCount() {
		return arSrc.size();
	}

	public String getItem(int position) {
		return arSrc.get(position).wData;
		
	}

	public long getItemId(int position) {
		return position;
	}
	
	// 각 항목의 뷰 생성
	public View getView(int position, View convertView, ViewGroup parent) {
		//System.out.println("리스트어댑터 클래스 진입");
		////System.out.println(arSrc.get(0).wCode);
		//System.out.println(arSrc.get(position).wData);
		final int pos = position;
		int imageCode;
		if (convertView == null) {
			convertView = Inflater.inflate(layout, parent, false);
		}
		
		// 날씨 코드를 받아서 인트값으로 변환, 인트값에 맞는 이미지 설정하기
		ImageView img = (ImageView)convertView.findViewById(R.id.weatherimg);
		
		imageCode = Integer.parseInt(arSrc.get(position).wCode);
		if(imageCode == 0 || imageCode == 2) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_hurricane));
		}
		else if(imageCode == 1) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_thundersun));
		}
		else if(imageCode == 3 || imageCode == 4 || imageCode == 45 || imageCode == 47 || imageCode == 37) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_thunder));
		}
		else if(imageCode == 5 || imageCode == 6 || imageCode == 10 || imageCode == 35) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_hailalt));
		}
		else if(imageCode == 7 || imageCode == 8 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_snowflakes));
		}
		else if(imageCode == 9 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_drizzle));
		}
		else if(imageCode == 11 || imageCode == 12 || imageCode == 17 || imageCode == 40 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_rainalt));
		}
		else if(imageCode == 13 || imageCode == 18 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_snowalt));
		}
		else if(imageCode == 15 || imageCode == 16 || imageCode == 41 || imageCode == 43 || imageCode == 46 || imageCode == 42 || imageCode == 14 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_snow));
		}
		else if(imageCode == 19 || imageCode == 21 || imageCode == 22 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_fogalt));
		}
		else if(imageCode == 20) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_fog));
		}
		else if(imageCode == 44) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_cloud));
		}
		else if(imageCode == 23 || imageCode == 24 || imageCode == 25 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_wind));
		}
		
		else if(imageCode == 26 || imageCode == 38 || imageCode == 39) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_whitecloud));
		}
		else if(imageCode == 27 || imageCode == 29) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_cloudmoon));
		}
		else if(imageCode == 28 || imageCode == 30 ) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_cloudsun));
		}
		else if(imageCode == 31 || imageCode == 32 || imageCode == 33 || imageCode == 34 || imageCode == 36) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_sun));
		}
		else if(imageCode == 3200) {
			img.setImageDrawable(maincon.getResources().getDrawable(R.drawable.ic_q));
		}
		//

		TextView txt1 = (TextView)convertView.findViewById(R.id.listtextview1);
		TextView txt2 = (TextView)convertView.findViewById(R.id.listtextview2);
		
		String changedTextOthers = cd.CodeChange(arSrc.get(position).wCode);
		txt1.setText(changedTextOthers);
		txt2.setText(arSrc.get(position).wData + '\n' + arSrc.get(position).wLow + "˚C / " + arSrc.get(position).wHigh + "˚C");
		//txt2.setText(arSrc.get(position).wData);
		
		return convertView;
	}

}
